<template>
  <div class="search-result-container">
    <van-cell-group>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in resultList"
          :key="item.art_id"
          :title="item.title"
          :to="{ name: 'article', params: { articleId: item.art_id } }"
        />
      </van-list>
    </van-cell-group>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  name: 'SearchResult',
  props: {
    searchValue: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      resultList: [],
      loading: false,
      finished: false,
      page: 1,
      perPage: 20
    }
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      const { data } = await getSearchResult(
        this.searchValue,
        this.page,
        this.perPage
      )
      // console.log(data)
      const results = data.data.results

      this.resultList.push(...results)
      this.loading = false

      // 如果还有数据，page加1，给下一次 load 做准备
      if (results.length) {
        this.page++
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
